//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include "CheckingDlg.h"
#include "FormUtilities.h"
#include "Calendar.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"

//---------------------------------------------------------------------------

__fastcall TfrmCheckingDlg::TfrmCheckingDlg(TComponent* Owner, TStrings * checkings)
        : TForm(Owner)
{
   TFormUtilities::LocateForm(( TForm*)Owner, this);

   this->Caption = "Add new checking";
   this->checkings = checkings;
   checking = NULL;
}

//---------------------------------------------------------------------------

__fastcall TfrmCheckingDlg::TfrmCheckingDlg(TComponent* Owner, TStrings * checkings, TChecking * checking)
        : TForm(Owner)
{
   TFormUtilities::LocateForm(( TForm*)Owner, this);
        
   this->Caption = "Update checking";
   this->checkings = checkings;
   this->checking = checking;

   edtLeft->Text   = checking->left;
   edtRight->Text  = checking->right;
   edtRemark->Text = checking->remark;

   if (( double)checking->date != 0)
      edtDate->Text = DateToStr( checking->date);
}

//---------------------------------------------------------------------------

void __fastcall TfrmCheckingDlg::btnOKClick(TObject *Sender)
{
   TDateTime date;

   if (( edtLeft->Text.Length() == 0) && ( edtRight->Text.Length() == 0))
   {
      MessageDlg( "At least one checking should be entered !",
                  mtError,
                  TMsgDlgButtons() << mbOK,
                  0);
                  
      edtRight->SetFocus();
      ModalResult = mrNone;
      return;
   }

   try
   {
      if ( edtDate->Text.Length() == 0)
         date = 0;
      else
         date = StrToDate( edtDate->Text);

      if ( checking == NULL)
      {
         checking = new TChecking( edtLeft->Text, edtRight->Text, edtRemark->Text, date);
         checkings->AddObject( "", checking);
      }
      else
      {
         checking->left   = edtLeft->Text;
         checking->right  = edtRight->Text;
         checking->remark = edtRemark->Text;
         checking->date   = date;
      }
   }
   catch( EConvertError * e)
   {
      MessageDlg( "Illegal date",
                  mtError,
                  TMsgDlgButtons() << mbOK,
                  0);

      ModalResult = mrNone;
   }
}
//---------------------------------------------------------------------------
void __fastcall TfrmCheckingDlg::btnDateClick(TObject *Sender)
{
   TfrmCalendar * frmCalendar;

   frmCalendar = new TfrmCalendar( this);

   try
   {
      if ( edtDate->Text.Length() > 0)
         frmCalendar->calendar->Date = StrToDate( edtDate->Text);

      if ( frmCalendar->ShowModal() == mrOk)
         edtDate->Text = DateToStr( frmCalendar->calendar->Date);
   }
   __finally
   {
      delete frmCalendar;
   }        
}
//---------------------------------------------------------------------------

